package week_07;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class SuperMarktSell_145 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(new BufferedInputStream(System.in));

        while (scanner.hasNextInt()) {
            int n = scanner.nextInt();
            List<int[]> goodList = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                goodList.add(new int[]{scanner.nextInt(), scanner.nextInt()});
            }
            int[] fa = new int[10001];
            for (int i = 0; i <= 10000; i++) fa[i] = i;
            int ans = sell(goodList, n, fa);
            System.out.println(ans);
        }
    }

    public static int sell(List<int[]> goods, int n, int[] fa) {
        Collections.sort(goods, (good1, good2) -> good1[0] == good2[0] ?
                good1[1] - good2[1] : good2[0] - good1[0]);

        int ans = 0;
        for (int i = 0; i < n; i++) {
            int profit = goods.get(i)[0];
            int day = goods.get(i)[1];
            int lastDay = find(day,fa);
            if (lastDay > 0) {
                ans += profit;
                fa[lastDay] = lastDay - 1;
            }
        }
        return ans;
    }

    public static int find(int x, int[] fa) {
        return (x == fa[x]) ? x : (fa[x] = find(fa[x], fa));
    }

    public static int sell1(List<int[]> goods, int n) {
        Collections.sort(goods, (good1, good2) -> good1[0] == good2[0] ?
                good1[1] - good2[1] : good2[0] - good1[0]);
        int[] res = new int[10001];
        for (int i = 0; i < n; i++) {
            int index = goods.get(i)[1];
            while (index > 0 && res[index] > 0) index--;
            if (index > 0) res[index] = goods.get(i)[0];
        }
        int ans = 0;
        for (int val : res) ans += val;
        return ans;
    }
}
